package com.tucc.hello.leetcode.no1_400.no1_200.no81_100.no99;

import com.tucc.hello.tree.Main;
import com.tucc.hello.tree.TreeNode;

import java.util.Arrays;

/**
 * @author tucc
 * @description todo
 * @package com.tucc.hello.leetcode
 * @date 2021/5/20
 **/
public class Solution2 {

    public static void main(String[] args) {
        Solution2 solution = new Solution2();
        TreeNode root = Main.makeTree(Arrays.asList(3, 1, 4, null, null, 2));
        solution.recoverTree(root);
        TreeNode.printMiddleOrder(root);
    }


    public void recoverTree(TreeNode root) {
        if (root.left != null) {
            if (root.left.val > root.val) {
                int temp = root.val;
                root.val = root.left.val;
                root.left.val = temp;
                recoverTree(root.left);
            }
        }
        if (root.right != null) {
            if (root.right.val < root.val) {
                int temp = root.val;
                root.val = root.right.val;
                root.right.val = temp;
                recoverTree(root.right);
            }
        }
    }


}
